/*
Copyright (c) 2012 the VATSIM Connector project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef __XPINN__PLUGIN_H__
#define __XPINN__PLUGIN_H__

#include "xpinn.h"
#include "callbacks.h"
#include "datarefs.h"
#include "connectdlg.h"
#include "menus.h"
#include "msgconsole.h"
#include "planes.h"

class VconConnection;
struct VconMsgPlanePosition;
struct VconMsgWeather;
struct VconMsgText;
struct VconMsgSquawk;

class XPInnPlugin : public XPInnConnectDialog::Observer
{
public:
  XPInnPlugin();
  ~XPInnPlugin();
  bool enable();
  void disable();

  void updateAirplaneCount();
  void updateUserAircraftType();

  void handleMessagePP(VconMsgPlanePosition const&);
  void handleMessageWX(VconMsgWeather const&);
  void handleMessageMG(VconMsgText const&);
  void handleMessageSQ(VconMsgSquawk const&);

  virtual void connect(int localPort, int remotePort, char const* remoteAddr,
    char const* user, char const* pass);

private:
  VconConnection* m_conn;
  XPInnMessageConsole m_msgConsole;
  XPInnMenuItem m_xpdrModeCMenuItem;
  XPInnPlanes m_planes;

  void init();

  XPInnFlightLoopCallback m_deferredInitCallback;
  bool deferredInitCallback(float myElapsed, float loopElapsed);

  XPInnFlightLoopCallback m_frameCallback;
  bool frameCallback(float myElapsed, float loopElapsed);

  XPInnFlightLoopCallback m_infrequentCallback;
  bool infrequentCallback(float myElapsed, float loopElapsed);

  void connectToConnectorMenuItemCallback();
  void disconnectFromConnectorMenuItemCallback();
  void xpdrModeCMenuItemCallback();

  void disconnect();

  void sendMessageOP();
  void sendMessageRX(int radio);
  void sendMessageCQ();

  int m_xpdrCode;
  int m_xpdrMode;
  bool m_xpdrIdent;
  int m_com1Freq;
  int m_com2Freq;

  class
  {
  public:
    XPInnDataRef<xplane::data::sim::cockpit::radios::transponder_code> transponder_code;
    XPInnDataRef<xplane::data::sim::cockpit::radios::transponder_mode> transponder_mode;
    XPInnDataRef<xplane::data::sim::cockpit::radios::transponder_id> transponder_id;
    XPInnDataRef<xplane::data::sim::cockpit::radios::com1_freq_hz> com1_freq_hz;
    XPInnDataRef<xplane::data::sim::cockpit::radios::com2_freq_hz> com2_freq_hz;
  } m_datarefs;
};

#endif //__XPINN__PLUGIN_H__